<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="auto" size="small">
      <el-form-item :label="$t('menu1')" prop="menuName">
        <el-input
          v-model="queryParams.menuName"
          clearable
          :placeholder="$t('input')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--      <el-form-item label="父菜单ID" prop="parentId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.parentId"-->
      <!--          placeholder="请输入父菜单ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">{{$t("search")}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t("reset")}}</el-button>
      </el-form-item>
    </el-form>

    <!--    <el-row :gutter="10" class="mb8">-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="primary"-->
    <!--          plain-->
    <!--          icon="el-icon-plus"-->
    <!--          size="mini"-->
    <!--          @click="handleAdd"-->
    <!--          v-hasPermi="['system:receptionMenu:add']"-->
    <!--        >{{$t("add")}}</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="success"-->
    <!--          plain-->
    <!--          icon="el-icon-edit"-->
    <!--          size="mini"-->
    <!--          :disabled="single"-->
    <!--          @click="handleUpdate"-->
    <!--          v-hasPermi="['system:receptionMenu:edit']"-->
    <!--        >{{$t("edit")}}</el-button>-->
    <!--      </el-col>-->
    <!--      <el-col :span="1.5">-->
    <!--        <el-button-->
    <!--          type="danger"-->
    <!--          plain-->
    <!--          icon="el-icon-delete"-->
    <!--          size="mini"-->
    <!--          :disabled="multiple"-->
    <!--          @click="handleDelete"-->
    <!--          v-hasPermi="['system:receptionMenu:remove']"-->
    <!--        >{{$t("delete")}}</el-button>-->
    <!--      </el-col>-->
    <!--&lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
    <!--&lt;!&ndash;        <el-button&ndash;&gt;-->
    <!--&lt;!&ndash;          type="warning"&ndash;&gt;-->
    <!--&lt;!&ndash;          plain&ndash;&gt;-->
    <!--&lt;!&ndash;          icon="el-icon-download"&ndash;&gt;-->
    <!--&lt;!&ndash;          size="mini"&ndash;&gt;-->
    <!--&lt;!&ndash;          @click="handleExport"&ndash;&gt;-->
    <!--&lt;!&ndash;          v-hasPermi="['system:receptionMenu:export']"&ndash;&gt;-->
    <!--&lt;!&ndash;        >{{$t("export")}}</el-button>&ndash;&gt;-->
    <!--&lt;!&ndash;      </el-col>&ndash;&gt;-->
    <!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--    </el-row>-->

    <el-table v-loading="loading" :data="receptionMenuList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55"/>
      <!--      <el-table-column label="菜单编号" align="center" prop="menuId" />-->
      <el-table-column align="center" :label="$t('menu1')" prop="menuName"/>
      <!--      <el-table-column label="父菜单名称" align="center" prop="remark" />-->

      <el-table-column align="center" class-name="small-padding fixed-width" :label="$t('controls')">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['system:receptionMenu:edit']"
            icon="el-icon-edit"
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >{{$t("edit")}}
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['system:receptionMenu:remove']"-->
          <!--          >{{$t("delete")}}</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

    <!-- 添加或修改前台菜单权限对话框 -->
    <el-dialog :title="title" :visible.sync="open" append-to-body width="700px">
      <el-form ref="form" :model="form" :rules="rules" label-width="auto">
        <el-form-item :label="$t('menu1')" prop="menuName">
          <el-input v-model="form.menuName" :placeholder="$t('input')"/>
        </el-form-item>
        <!--    -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t("ok")}}</el-button>
        <el-button @click="cancel">{{$t("cancel")}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addReceptionMenu,
  delReceptionMenu,
  getReceptionMenu,
  listReceptionMenu,
  updateReceptionMenu
} from "@/api/system/receptionMenu";

export default {
  name: "ReceptionMenu",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 前台菜单权限表格数据
      receptionMenuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        menuName: null,
        parentId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        menuName: [
          {required: true, message: this.$t('menu11'), trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询前台菜单权限列表 */
    getList() {
      this.loading = true;
      listReceptionMenu(this.queryParams).then(response => {
        this.receptionMenuList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        menuId: null,
        menuName: null,
        parentId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.menuId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加前台菜单权限";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const menuId = row.menuId || this.ids
      getReceptionMenu(menuId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('menu2');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.menuId != null) {
            updateReceptionMenu(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('modifiedSuccessfully'));
              this.open = false;
              this.getList();
            });
          } else {
            addReceptionMenu(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('newSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const menuIds = row.menuId || this.ids;
      this.$modal.confirm('是否确认删除前台菜单权限编号为"' + menuIds + this.$t('dataItem')).then(function () {
        return delReceptionMenu(menuIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('successfullyDeleted'));
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/receptionMenu/export', {
        ...this.queryParams
      }, `receptionMenu_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
